package com.lonely.fx;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

/**
 * @date: 2022-07-06 23:06
 * @author: ztkj-hzb
 * @description:
 **/
public class 添加快捷键 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     * setAccelerator() 设置快捷键
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        BorderPane borderPane = new BorderPane();

        MenuBar menuBar = new MenuBar();
        menuBar.setStyle("-fx-background-color: pink");

        borderPane.setTop(menuBar);


        //文本前加图片
        ImageView imageView = new ImageView("icons/lbxx.png");
        imageView.setFitWidth(20);
        imageView.setFitHeight(20);
        //设置保持纵横比
        imageView.setPreserveRatio(true);
        Menu menu1 = new Menu("File", imageView);
        menuBar.getMenus().addAll(menu1);


        //添加菜单项
        MenuItem newItem = new MenuItem("New");
        ImageView mlaImage = new ImageView("icons/mla.png");
        mlaImage.setFitWidth(20);
        mlaImage.setFitHeight(20);
        MenuItem openItem = new MenuItem("Open", mlaImage);
        MenuItem settingsItem = new MenuItem("Settings");

        menu1.getItems().addAll(newItem, openItem, settingsItem);

        //TODO: 设置快捷键--且会显示在菜单项中
        newItem.setAccelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN));


        //设置事件
        newItem.setOnAction(event -> {
            System.out.println("点击了New");
        });


        Scene scene = new Scene(borderPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(500);
        primaryStage.setHeight(400);
        primaryStage.show();


    }
}
